package com.ly.lypopupwindowdemo.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.ly.lypopupwindowdemo.R;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * Created by liying on 2017/6/8.
 * 鸿洋原博客
 * http://blog.csdn.net/lmj623565791/article/details/24252901
 */

public class FirstCustomView extends View {

    private String mTitleText;
    private int mTitleColor;
    private int mTitleTextSize;

    private Rect mBound;
    private Paint mPaint;


    public FirstCustomView(Context context) {
        this(context, null);
    }

    public FirstCustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FirstCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        /**
         * 获得我们自定义的属性
         */
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FirstCustomView, defStyleAttr, 0);

        mTitleText = typedArray.getString(R.styleable.FirstCustomView_titleText);
        mTitleColor = typedArray.getColor(R.styleable.FirstCustomView_titleColor, Color.BLACK);
        mTitleTextSize = typedArray.getDimensionPixelSize(R.styleable.FirstCustomView_titleSize, 16);

        typedArray.recycle();
        //获得绘制文本的宽高
        mPaint = new Paint();
        mPaint.setTextSize(mTitleTextSize);
        // mPaint.setColor(mTitleColor);
        mBound = new Rect();
        mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mTitleText = randomText();
                postInvalidate();
            }
        });


    }

    /**
     * Random random = new Random();
     * random.nextInt(100); //100是不包含在内的，只产生0~99之间的数。
     * 该方法想要实现 产生随机四位数
     */

    private String randomText() {

        Random random = new Random();
        Set<Integer> set = new HashSet<>();
        while (set.size() < 4) {
            int radomInt = random.nextInt(10);
            set.add(radomInt);
        }
        StringBuilder sb = new StringBuilder();
        for (Integer i : set) {
            sb.append(i + "");
        }
        return sb.toString();

    }


    /**
     * 系统默认帮我们测量的是Match_parent,如果设置了具体的值就会显示具体的值
     * 如果设置的是wrap_content或者match_parent，系统会直接按照match_parent来显示
     * 所以就需要我们重写onMeasure()方法
     * <p>
     * 重写之前我们需要了解MeasureSpec的specMode有三种类型
     * 1、EXACLY:设置了明确的值或者是match_parent;
     * 2、AT_MOST:表示子布局限制在一个最大值内，一般是设置wrap_content;
     * 3、UNSPECIFIED:(未指明的)表示子布局想要多大就有多大，一般不使用
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            mPaint.setTextSize(mTitleTextSize);
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
            float textWidth = mBound.width();
            int dirserd = (int) (getPaddingLeft() + textWidth + getPaddingRight());
            width = dirserd;

        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            mPaint.setTextSize(mTitleTextSize);
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
            float textHeight = mBound.height();
            int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
            height = desired;
        }

        setMeasuredDimension(width, height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
        mPaint.setColor(mTitleColor);
        canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);

    }

    private int dp2px(float dp) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    private int sp2px(float spValue) {
        float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

}
